package com.winit.psc.manager;

import com.winit.commons.util.ObjectToString;
import com.winit.commons.util.SeqGenerate;
import com.winit.psc.model.MatrixPsc;
import com.winit.psc.model.PmsCategory;
import com.winit.psc.repository.PmsCategoryRepository;
import com.winit.psc.repository.PscOperations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangjian on 2017/12/1.
 */
@Component
public class PmsCategoryManager {
    private static Logger log = LoggerFactory.getLogger(PmsCategoryManager.class);

    @Autowired
    private PmsCategoryRepository repository;
    @Autowired
    private PscOperations operations;

    public void importPmsCategory(String type) {
        //1、先查询parent id 为0 的根节点
        List<PmsCategory> roots = repository.findAllByTypeAndParent(type, 0);
        if (roots.size() == 0) {
            log.info("com.winit.psc.manager.PmsCategoryManager.importPmsCategory roots为空 type=" + type);
            return;
        }
        MatrixPsc psc = addRoot(type);
        log.info("com.winit.psc.manager.PmsCategoryManager.importPmsCategory roots.size=" + roots.size() + " type=" + type);
        for (PmsCategory node : roots) {
            dealNode(node, "", psc.getId());
        }
    }

    //添加根节点
    private MatrixPsc addRoot(String type) {
        MatrixPsc psc = new MatrixPsc();
        psc.setId(SeqGenerate.createId("CA"));
        if ("P".equals(type)) {
            psc.setName("psc");
            psc.setCode("root_psc");
        } else if ("S".equals(type)) {
            psc.setName("su");
            psc.setCode("root_su");
        }
        psc.setParent_id("0");
        psc.setStatus("ONLINE");
        psc.setType(type);
        psc.setPath("/");
        operations.updatePsc(psc);
        return psc;
    }

    private void dealNode(PmsCategory node, String path, String parentId) {
        if (StringUtils.isEmpty(path)) {
            path = node.getCATEGORY_NAME();
        } else {
            path = path + "/" + node.getCATEGORY_NAME();
        }
        MatrixPsc psc = PmsToMatrix(node, path, parentId);
        List<PmsCategory> child = repository.findAllByTypeAndParent(node.getCATEGORY_TYPE(), node.getID());
        if (child.size() == 0) {
            operations.updatePsc(psc);
            return;
        }
        List<String> list = new ArrayList<>();
        for (PmsCategory n : child) {
            list.add(n.getCATEGORY_CODE());
            dealNode(n, path, psc.getId());
        }
        psc.setChilds(list);
        operations.updatePsc(psc);
    }

    private MatrixPsc PmsToMatrix(PmsCategory node, String path, String parentId) {
        MatrixPsc psc = new MatrixPsc();
        psc.setId(SeqGenerate.createId("CA"));
        psc.setName(node.getCATEGORY_NAME());
        psc.setCode(node.getCATEGORY_CODE());
        psc.setParent_id(parentId);
        psc.setStatus("ONLINE");
        psc.setType(node.getCATEGORY_TYPE());
        psc.setPath(path);
        return psc;
    }

}
